package com.example.voice;
import android.app.Activity;
import android.app.Service;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
    private Button  btnUpper=null, btnLower=null;
    private ToggleButton tbMute=null;
    private MediaPlayer mediaPlayer=null; //声频
    private AudioManager audioManager=null; //音频
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        audioManager=(AudioManager)getSystemService(Service.AUDIO_SERVICE);
        btnUpper=(Button)findViewById(R.id.btnUpper);
        btnLower=(Button)findViewById(R.id.btnLower);
        btnUpper.setOnClickListener(listener);
        btnLower.setOnClickListener(listener);
        tbMute=(ToggleButton)findViewById(R.id.tbMute);
        tbMute.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                audioManager.setStreamMute(AudioManager.STREAM_MUSIC, !isChecked); //设置是否静音
            }
        });
    }
    View.OnClickListener listener=new View.OnClickListener(){
        public void onClick(View v) {
            @SuppressWarnings("unused")
            Button btn=(Button)v;
            switch (v.getId()) {
                case R.id.btnUpper://增多音量
                    //adjustStreamVolume: 调整指定声音类型的音量
                    audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                            AudioManager.ADJUST_RAISE,
                            AudioManager.FLAG_SHOW_UI);  //调高声音
                    break;
                case R.id.btnLower://减少音量
                    //第一个参数：声音类型
                    //第二个参数：调整音量的方向
                    //第三个参数：可选的标志位
                    audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                            AudioManager.ADJUST_LOWER,
                            AudioManager.FLAG_SHOW_UI);//调低声音
                    break;
            }
        }
    };
}